## Version Control ##
# Version 1.0, Initial Release 04/08/11  Fred Grandlienard

## Imports ##
from PyDevice import *
from Py_Lauterbach import *

## Local Functions ##

TTB_Print = 1



def ReadMemory(proc, signal, low, high, tstNum, des):
	
    if proc == "MCP1": 
        (error, value) = ReadMemValue(PROCESSOR,1, signal)
    elif proc == "MCP2": 
        (error, value) = ReadMemValue(PROCESSOR,2, signal)
    else : 
        (error, value) = ReadMemValue(PROCESSOR,0, signal)
		
    if error !=0 : 
	    value = -1
	
    if TTB_Print: 
        TTB_Log(MISC, "Lauterbach Memory Read: %s - %s = %s (l %s, h %s)" % (proc, signal, str(value), str(low), str(high)))
		
    varName = "%s - %s %s" % (proc,  signal, tstNum)
    TTB_Results(varName, 0, des, value, low, high)	
    return value	

def WriteMemory(proc, signal, value):	
    if proc == "MCP1": 
        WriteMemValue(PROCESSOR, 1, signal, value) 
    elif proc == "MCP2": 
        WriteMemValue(PROCESSOR, 2, signal, value) 
    else : 
        WriteMemValue(PROCESSOR, 0, signal, value)
    
    if TTB_Print: 
        TTB_Log(MISC, "Lauterbach Memory Write: %s - %s = %s" % (proc, signal, str(value)))
		
    Wait(2000)

def GetValue(proc, signal):	
    if proc == "MCP1": 
        (error, value) = ReadMemValue(PROCESSOR,1, signal)
    elif proc == "MCP2": 
        (error, value) = ReadMemValue(PROCESSOR,2, signal)
    else : 
        (error, value) = ReadMemValue(PROCESSOR,0, signal)
		
    if error !=0 : 
	    value = -1
	
    return value
	
		
		
def isIncreasing(processor, signal, testNum, samples, interval, min, max):
    var_old = 0
    error   = 0 
	
    if processor == "MCP1":
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,1, signal)
            if var <= var_old and var != max or retc != 0 or var < min or var > max: 
                error = 1
                break
            var_old = var
            Wait(interval)
    elif processor == "MCP2": 
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,2, signal)
            if var <= var_old and var != max or retc != 0 or var < min or var > max: 
                error = 1
                break
            var_old = var
            Wait(interval)
    else : 
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,0, signal)
            if var <= var_old and var != max or retc != 0 or var < min or var > max: 
                error = 1
                break
            var_old = var
            Wait(interval)
			
    varName = "%s - %s %s" %(processor, signal, testNum)
    TTB_Results(varName,0, "Increment Test", error, 0, 0) 
	
    if retc == -1:
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s could not be read correctly (ERROR)" % (processor, signal))
        return -1
    elif error == 1: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is NOT increasing correctly (ERROR)" % (processor, signal))	
        return -2
    else: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is increasing correctly" % (processor, signal))
		
        return 0
	


def isDecreasing(processor, signal, testNum, samples, interval, min, max):
    var_old = 0
    error   = 0 
	
    if processor == "MCP1":
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,1, signal)
            if var >= var_old and var != max or retc != 0 or var < min or var > max: 
                error = 1
                break
            var_old = var
            Wait(interval)
    elif processor == "MCP2": 
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,2, signal)
            if var >= var_old and var != max or retc != 0 or var < min or var > max: 
                error = 1
                break
            var_old = var
            Wait(interval)
    else : 
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,0, signal)
            if var >= var_old and var != max or retc != 0 or var < min or var > max: 
                error = 1
                break
            var_old = var
            Wait(interval)
				
    varName = "%s - %s %s" %(processor, signal, testNum)
    TTB_Results(varName,0, "Decreasing Test", error, 0, 0) 
    if retc == -1:
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s could not be read correctly (ERROR)" % (processor, signal))
        return -1
    elif error == 1: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is NOT decreasing correctly (ERROR)" % (processor, signal))	
        return -2
    else: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is decreasing correctly" % (processor, signal))
		
        return 0


def isUpdating(processor, signal, testNum, samples, interval):
    error   = 0 
	
    if   processor == "MCP1":
        (retc, var_old) = ReadMemValue(PROCESSOR, 1, signal)
        Wait(interval)
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,1, signal)
            if var == var_old or retc != 0: 
                error = 1
            elif var != var_old:
                error = 0
            var_old = var
            Wait(interval)
    elif processor == "MCP2": 
        (retc, var_old) = ReadMemValue(PROCESSOR, 2, signal)
        Wait(interval)	
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,2, signal)
            if var == var_old or retc != 0: 
                error = 1
            elif var != var_old:
                error = 0
            var_old = var
            Wait(interval)
    else : 
        (retc, var_old) = ReadMemValue(PROCESSOR, 0, signal)
        Wait(interval)	
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,0, signal)
            if var == var_old or retc != 0: 
                error = 1
            elif var != var_old:
                error = 0
            var_old = var
            Wait(interval)
			
				
    varName = "%s - %s %s" %(processor, signal, testNum)
    TTB_Results(varName,0, "Updating Test", error, 0, 0) 	
	
    if retc == -1:
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s could not be read correctly (ERROR)" % (processor, signal))
        return -1
    elif error == 1: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is NOT updating (ERROR)" % (processor, signal))	
        return -2
    else: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is updating correctly" % (processor, signal))		
        return 0

	
	
def	notUpdating(processor, signal, testNum, samples, interval):
    error   = 0 
	
    if processor == "MCP1":
        (retc, var_old) = ReadMemValue(PROCESSOR, 1, signal)
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,1, signal)
            if var != var_old or retc != 0: 
                error = 1
                break
            var_old = var
            Wait(interval)
    elif processor == "MCP2": 
        (retc, var_old) = ReadMemValue(PROCESSOR, 2, signal)	
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,2, signal)
            if var != var_old or retc != 0: 
                error = 1
                break
            var_old = var
            Wait(interval)
    else : 
        (retc, var_old) = ReadMemValue(PROCESSOR, 0, signal)	
        for x in range(0,samples):
            (retc, var) = ReadMemValue(PROCESSOR,0, signal)
            if var != var_old or retc != 0: 
                error = 1
                break
            var_old = var
            Wait(interval)
			
	
    varName = "%s - %s %s" %(processor, signal, testNum)
    TTB_Results(varName,0, "Not Updating Test", error, 0, 0) 	
	
	
    if retc == -1:
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s could not be read correctly (ERROR)" % (processor, signal))
        return -1
    elif error == 1: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s IS updating (ERROR)" % (processor, signal))	
        return -2
    else: 
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s's %s is not updating" % (processor, signal))		
        return 0	